package com.wdd.datasource;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.jta.atomikos.AtomikosDataSourceBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import com.mysql.jdbc.jdbc2.optional.MysqlXADataSource;

@Configuration  
@MapperScan(basePackages = "com.wdd.web.test02", sqlSessionTemplateRef = "test2SqlSessionTemplate")
public class TestMyBatisConfig2 {
    @Bean(name = "test2DataSource")  
	 public DataSource testDataSource(DBConfig2 testConfig) throws SQLException{
		MysqlXADataSource mysqlXaDataSource = new MysqlXADataSource();  
		mysqlXaDataSource.setUrl(testConfig.getUrl());  
        mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);  
        mysqlXaDataSource.setPassword(testConfig.getPassword());  
        mysqlXaDataSource.setUser(testConfig.getUsername());  
        mysqlXaDataSource.setPinGlobalTxToPhysicalConnection(true);  
  
        AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();  
        xaDataSource.setXaDataSource(mysqlXaDataSource);  
        xaDataSource.setUniqueResourceName("test2DataSource");  
  
        xaDataSource.setMinPoolSize(testConfig.getMinPoolSize());  
        xaDataSource.setMaxPoolSize(testConfig.getMaxPoolSize());  
        xaDataSource.setMaxLifetime(testConfig.getMaxLifetime());  
        xaDataSource.setBorrowConnectionTimeout(testConfig.getBorrowConnectionTimeout());  
        xaDataSource.setLoginTimeout(testConfig.getLoginTimeout());  
        xaDataSource.setMaintenanceInterval(testConfig.getMaintenanceInterval());  
        xaDataSource.setMaxIdleTime(testConfig.getMaxIdleTime());  
        xaDataSource.setTestQuery(testConfig.getTestQuery());  
        return xaDataSource; 
	 }
	 
	    @Bean(name = "test2SqlSessionFactory")
	    public SqlSessionFactory testSqlSessionFactory(@Qualifier("test2DataSource") DataSource dataSource)throws Exception {  
	        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();  
	        bean.setDataSource(dataSource);
	        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
	        try {
	            bean.setMapperLocations(resolver.getResources("classpath:mybatis/mapper/*.xml"));
	            return bean.getObject();
	        } catch (Exception e) {
	            e.printStackTrace();
	            throw new RuntimeException(e);
	        }
	    }  
	  
	    @Bean(name = "test2SqlSessionTemplate")
	    public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {  
	        return new SqlSessionTemplate(sqlSessionFactory);
	    }  
}
